package com.example.treehole.ui.component

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBackIosNew
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
import com.example.treehole.LocalSquareViewModel
import com.example.treehole.ui.model.PostType
import com.example.treehole.ui.theme.Cyan
import com.example.treehole.ui.theme.SongTi
import com.example.treehole.ui.theme.darkCyan

@Composable
fun AppTopBar(navController: NavController) {
    val squareViewModel = LocalSquareViewModel.current
    val selectedTag by squareViewModel.selectedTag.collectAsState()
    val currentDestination = navController.currentBackStackEntryAsState().value?.destination
    val title =
        when (currentDestination?.route) {
            "treehole" -> "梧桐树洞"
            "square" -> {
                "广场" + if (selectedTag != PostType.NO_TYPE)
                    ("-" + squareViewModel.getTagNameByPostType())
                else ""
            }
            "myself" -> "我的"
            "createPost" -> "发布帖子"
            "myPosts" -> "我的帖子"
            "myComments" -> "我的评论"
            else -> "帖子详情"
        }
    val isPostDetailRoute = currentDestination?.route == "postDetail"

    Box(
        modifier =
        Modifier
            .fillMaxWidth()
            .background(
                brush =
                Brush.horizontalGradient(
                    colors = listOf(Cyan, darkCyan)
                )
            )
            .height(80.dp),
        contentAlignment = Alignment.BottomStart
    ) {
        if (!isPostDetailRoute)
            Text(
                text = title,
                color = Color.White,
                style = MaterialTheme.typography.headlineMedium,
                modifier = Modifier.padding(start = 16.dp, bottom = 4.dp),
                fontFamily = SongTi
            )
        else
            IconButton(onClick = { navController.popBackStack() }) {
                Icon(
                    imageVector = Icons.Default.ArrowBackIosNew,
                    contentDescription = "Back",
                    modifier = Modifier.size(28.dp),
                    tint = Color.White
                )
            }
    }
}
